package com.li.onjava8.concurrent.cf.example.pizza;

import com.li.onjava8.concurrent.utils.Timer;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class CompletablePizza {

    public static CompletableFuture<Pizza> makeCF(Pizza pizza) {
        return CompletableFuture.completedFuture(pizza)
                .thenApplyAsync(Pizza::roll)
                .thenApplyAsync(Pizza::sauce)
                .thenApplyAsync(Pizza::cheese)
                .thenApplyAsync(Pizza::toppings)
                .thenApplyAsync(Pizza::bake)
                .thenApplyAsync(Pizza::slice)
                .thenApplyAsync(Pizza::box);
    }

    public static void show(CompletableFuture<Pizza> cf){
        try{
            System.out.println(cf.get());
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }


    public static void main(String[] args) {
        Timer timer = new Timer();
        List<CompletableFuture<Pizza>> pizzaCF = IntStream.range(0, 10)
                .mapToObj(Pizza::new)
                .map(CompletablePizza::makeCF)
                .collect(Collectors.toList());
        System.out.println(timer.duration());
        pizzaCF.forEach(CompletablePizza::show);
        System.out.println(timer.duration());

    }
}
